var BASE_URL = 'http://jinzhidata.com/v2'

var sceneId = '';

function formatTime(timeVal, fmt) {
    timeVal = new Date(timeVal.replace(/^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}).*$/, '$1 $2 GMT+0000').replace(/-/g, '/'))
    var o = {
        "M+": timeVal.getMonth() + 1, //月份
        "d+": timeVal.getDate(), //日
        "h+": timeVal.getHours(), //小时
        "m+": timeVal.getMinutes(), //分
        "s+": timeVal.getSeconds(), //秒
        "q+": Math.floor((timeVal.getMonth() + 3) / 3), //季度
        "S": timeVal.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (timeVal.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}

//读取cookie
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)) {
        return unescape(arr[2]);
    }
    return null;
}

function checkLogin() {
    var intervalId = setInterval(function () {
        $.ajax({
            type: 'get',
            data: {sceneId: sceneId},
            dataType: 'json',
            url: BASE_URL + "/isWebLoginSuccess",
            success: function (response) {
                console.log(response)
                if (response.code === 0) {
                    console.log('登录成功')
                    clearInterval(intervalId)
                    layer.closeAll();
                    $(".is-login").show()
                    $(".unlogin").hide()
                    $("#user-logout").click(function () {
                        location.href = BASE_URL + "/logout"
                        document.cookie = ""
                    })
                }
            }
        })
    }, 1000);
    setTimeout(function () {
        clearInterval(intervalId)
    }, 1000 * 60 * 5);
}

function login() {
    $(".fade").show();
    $(".succ-pop").show();
    $.ajax({
        type: 'get',
        dataType: 'json',
        url: BASE_URL + "/webLogin",
        success: function (response) {
            var img = "<img style='width:85%;margin-left:20px;' src='" + response.data.qrCodeUrl + "'>";
            //iframe层
            //在这里面输入任何合法的js语句
            layer.open({
                type: 1, //Page层类型
                area: ['320px', '330px'],
                title: '微信扫码登录，获取更多免费信息',
                shade: 0.6, //遮罩透明度
                maxmin: false,//允许全屏最小化
                anim: 1, //0-6的动画形式，-1不开启
                content: img
            });
            sceneId = response.data.sceneId
            checkLogin()
        }
    });
}

function webCharge() {
    $.ajax({
        type: "get",
        url: BASE_URL + "/wallet/webCharge",
        contentType: 'application/json',
        dataType: "json",
        success: function (response) {
            if (response.code === 0) {
                var img = "<img style='width:85%;margin-left:20px;' src='" + response.data.qrCodeBase64 + "'>";
                layer.open({
                    type: 1, //Page层类型
                    area: ['320px', '330px'],
                    title: '微信扫码充值',
                    shade: 0.6, //遮罩透明度
                    maxmin: false,//允许全屏最小化
                    anim: 1, //0-6的动画形式，-1不开启
                    content: img
                });
            } else if (response.code === 1001) {
                layer.msg('您还没有登录！', {icon: 2}, function () {
                    login()
                })
            } else {
                alert(response.msg)
            }
        }
    })

}

$(function () {
    if (getCookie("accessToken")) {
        $(".is-login").show()
        $(".unlogin").hide()
        $("#user-logout").click(function () {
            location.href = BASE_URL + "/logout"
            document.cookie = ""
        })
        $.ajax({
            type: "get",
            url: BASE_URL + "/getUserInfo",
            contentType: 'application/json',
            dataType: "json",
            success: function (response) {
                if (response.code === 0) {
                    var data = response.data
                    $("#username1").html(data.userName+'<b style="margin-left:5px;" class="j-bottom"></b>')
                    $("#username").html(data.userName+'<b style="margin-left:5px;" class="j-bottom"></b>')
                }
            }
        })
    } else {
        $(".is-login").hide()
        $(".unlogin").show()
        $("#login").click(function () {
            login()
        })
    }

    // 加载分类
    $.ajax({
        url: BASE_URL + "/resource/type/list",
        type: "get",
        contentType: 'application/json',
        dataType: "json",
        success: function (response) {
            var data = response.data
            var html = '<i class="biad_sj_i"></i>';
            for (var i = 0; i < data.length; i++) {
                var href = BASE_URL + '/pc/resource_list.html?typeName=' + data[i].resourceTypeName
                html += '<a href="' + href + '" target="_blank">' + data[i].resourceTypeName + '</a>';
            }
            $("#detail").html(html)
            $("#detail1").html(html)
        }
    })

    $("#sousuo,#sousuo1,#sou").click(function (event) {
        var value = $("input[name=q]").val();
        if (value === "" || value === "请输入关键词") {
            alert("请输入关键词")
        } else {
            window.location.href = BASE_URL + "/pc/search_resource.html?resourceName=" + value;
        }
    });
})


